package com.da.javatest.leetCode;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @author chenlida
 * @date 2019/3/11 17:36
 * @description
 */
public class Code914 {
    public static void main(String[] args) {
        System.out.println("2".compareTo("210"));
        System.out.println(new Code914().hasGroupsSizeX(
            new int[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3,
                3, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 7, 7}));
    }

    public boolean hasGroupsSizeX(int[] deck) {
        if (deck.length < 2) {
            return false;
        }
        Map<Integer, Integer> map = new HashMap<>();
        for (int i : deck) {
            map.put(i, map.getOrDefault(i, 0) + 1);
        }
        if (map.size() == 1) {
            return true;
        }
        Set<Integer> keys = map.keySet();
        int count = -1;
        for (Integer key : keys) {
            Integer target = map.get(key);
            if (target == 1) {
                return false;
            }
            if (count == -1) {
                count = target;
                continue;
            }
            if (count != target) {
                int common = findCommon(count, target);
                if (count % common != 0 || target % common != 0) {
                    return false;
                }
            }
        }
        return true;
    }

    private int findCommon(int n1, int n2) {
        int min = Math.min(n1, n2);
        int k = 2;
        while (k != min) {
            if (n1 % k == 0 && n2 % k == 0) {
                return k;
            }
            k++;
        }
        return min;
    }
}
